#include "stdafx.h"
#include "fuListCtrl.h"

fuListCtrl::fuListCtrl(wxWindow * parent, 
                       wxWindowID id /*= wxID_ANY*/, 
                       const wxPoint& pos /*= wxDefaultPosition*/, 
                       const wxSize& size /*= wxDefaultSize*/, 
                       long style /*= wxLC_REPORT*/, 
                       const wxValidator& validator /*= wxDefaultValidator*/, 
                       const wxString& name /*= wxListCtrlNameStr*/ ) :
wxListCtrl(parent,id,pos,size,style,validator,name)
{

}

fuListCtrl::~fuListCtrl(void)
{
}

void fuListCtrl::AddColumn( const wxString & col )
{
    int cnt = GetColumnCount();
    InsertColumn(cnt,col);
}

long fuListCtrl::AddItem( void )
{
    int cnt = GetItemCount();
    return InsertItem(cnt,wxT(""));
}

void fuListCtrl::ResizeColumns( void )
{
    for(int cnt = 0; cnt < GetColumnCount(); cnt++)
        SetColumnWidth(cnt,wxLIST_AUTOSIZE_USEHEADER);
}

int fuListCtrl::FindItemByData( long dwData )
{
    for(int index = 0; index < GetItemCount(); index++)
    {
        long data = GetItemData(index);
        if(data == dwData)
            return index;
    }

    return ITEM_NOT_FOUND;
}

int fuListCtrl::FindItem( long column,const wxString &value )
{
    for(int index = 0; index < GetItemCount(); index++)
        if( GetItemText(index,column) == value )
            return index;

    return ITEM_NOT_FOUND;
}

int fuListCtrl::FindItem( const wxString & column , const wxString & value)
{
    for(int index = 0; index < GetItemCount(); index++)
        if( GetItemText(index,column) == value )
            return index;

    return ITEM_NOT_FOUND;
}

long fuListCtrl::GetSelectedItems( std::vector<int> & selected )
{
    selected.clear();
    for(int index = 0; index < GetItemCount(); index++){
        int state = GetItemState(index,wxLIST_STATE_SELECTED);
        if( state & wxLIST_STATE_SELECTED )
            selected.push_back(index);
    }
    return selected.size();
}

long fuListCtrl::GetSelectedItem( void )
{
    if( wxListCtrl::GetSelectedItemCount() >= 1 ){
        std::vector<int> selected;
        GetSelectedItems(selected);
        return selected[0];
    }
    return ITEM_NOT_FOUND;
}

long fuListCtrl::GetSelectedItemData(void)
{
    return GetItemData(GetSelectedItem());
}

wxString fuListCtrl::GetItemText( int index,int column )
{
    wxListItem info;
    info.SetId(index);
    info.SetColumn(column);
    info.SetMask(wxLIST_MASK_TEXT);
    GetItem(info);
    return info.GetText();
}

wxString fuListCtrl::GetItemText( int index,const wxString & column )
{
    for( int i = 0; i < GetColumnCount(); i++ )
        if( GetColumnName(i) == column )
            return GetItemText(index,i);

    return wxEmptyString;
}

void fuListCtrl::SetItemText( int index,int column, const wxString & value )
{
    SetItem(index,column,value);
}

void fuListCtrl::SetItemText( int index,const wxString & column, const wxString & value )
{
    for( int i = 0; i < GetColumnCount(); i++ )
    {
        if( GetColumnName(i) == column )
            SetItem(index,i,value);
    }
}

wxString fuListCtrl::GetColumnName( int column )
{
    wxListItem item;
    item.SetMask(wxLIST_MASK_TEXT );
    GetColumn(column,item);
    return item.GetText();
}

void fuListCtrl::SetItemSelected( int index )
{
    SetItemState(index,wxLIST_STATE_SELECTED,wxLIST_STATE_SELECTED);
    EnsureVisible(index);
}

void fuListCtrl::PostTextMsg( wxString msg )
{
    Connect(wxID_OK,wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( fuListCtrl::OnPostTextMsg ), NULL, this );

    wxCommandEvent * evt = new wxCommandEvent(wxEVT_COMMAND_MENU_SELECTED,wxID_OK);
    evt->SetString(msg);
    QueueEvent(evt);
}

void fuListCtrl::OnPostTextMsg( wxCommandEvent & evt )
{
    Disconnect(wxID_OK,wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( fuListCtrl::OnPostTextMsg ), NULL, this);

    using namespace cppx;

    std::vector<std::string> strvec;
    fuStringHelper(evt.GetString().c_str().AsChar()).Split("\n",strvec);
    for(size_t cnt = 0; cnt < strvec.size(); cnt++){
        std::string text = fuDateTime::now().toString(fuDateTime::DT_SECOND) + " : " + strvec[cnt];
        int m_index = AddItem();
        SetItemText(m_index,0,text);
        EnsureVisible(m_index);
    }
    ResizeColumns();
}